package com.ylyr.routerfunction.routers;

import com.ylyr.routerfunction.hadler.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.*;

import java.awt.*;

/**
 * @Explain:
 * @Author: 30374
 * @Date: 2021/4/24
 */
@Configuration
public class RouterAll {

    @Bean
    RouterFunction<ServerResponse> routerFunction(UserHandler userHandler) {
        //RequestPredicates.path("/user") 相当于类上的@RequestMapping("/user")
        return RouterFunctions.nest(RequestPredicates.path("/user"),
                //RequestPredicates.GET("/") 相当于方法上的@RequestMapping("/")
                RouterFunctions.route(RequestPredicates.GET("/"), userHandler::getAllUser)
                        .andRoute(RequestPredicates.POST("/")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::createUser)
                        .andRoute(RequestPredicates.DELETE("/{id}"), userHandler::deleteUserById)
        .andRoute(RequestPredicates.GET("/{id}"),userHandler::findUserById));

    }
}
